//
//  KVNHttpRequestEngine.m
//  Test1
//
//  Created by KelvinYeung on 15/7/22.
//  Copyright (c) 2015年 KelvinYeung. All rights reserved.
//

#import "KVNHttpRequestEngine.h"

#define HOME_HOT_LIST_URL @"/house/hotList.do"

@interface KVNHttpRequestEngine()

@end

@implementation KVNHttpRequestEngine


- (instancetype) init{
    self = [super init];
    return self;
}

-(MKNetworkOperation*) homeHotListWithType:(int) type
                                pageNumber:(int) pageNumber
                                  pageSize:(int) pageSize
                         completionHandler:(KVNResponseBlock)completionBlock
                              errorHandler:(MKNKErrorBlock)errorBlock
{
    

    NSDictionary *params  = @{@"pageNumber":[NSNumber numberWithInt:pageNumber],@"pageSize":[NSNumber numberWithInt:pageSize],@"params":@{@"activityCategoryId":[NSNumber numberWithInt:type]}};
    
    MKNetworkOperation *operation = [self operationWithPath:@"/house/hotList.do"
                                              params:params
                                          httpMethod:@"POST"];
    
    //设置请求头的ContentType为json
    [operation setHeader:@"Content-Type" withValue:@"application/json;encoding:utf-8"];
    //设置发送的数据为json格式
    
    [operation setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
    [operation addCompletionHandler:^(MKNetworkOperation *completedOperation)
     {
         
         if([completedOperation isCachedResponse]) {
            // DLog(@"Data from cache %@", [completedOperation responseString]);
         }
         else {
             //DLog(@"Data from server %@", [completedOperation responseString]);
         }
         //返回数据
         completionBlock(completedOperation);
         
     }errorHandler:^(MKNetworkOperation *errorOp, NSError* error) {
         //DLog(@"REQUEST ERROR:\n%@",[[errorOp readonlyRequest] allHTTPHeaderFields ]);
         errorBlock(error);
     }];
    
    [self enqueueOperation:operation];
    
    return operation;
    

}



- (MKNetworkOperation *) postWithPath:(NSString *)path json:(NSDictionary *)json completionHandler:(KVNResponseBlock)completionBlock errorHandler:(MKNKErrorBlock)errorBlock{

   
    MKNetworkOperation *operation = [self operationWithPath:path
                                                     params:json
                                                 httpMethod:@"POST"];
    
    //设置请求头的ContentType为json
    [operation setHeader:@"Content-Type" withValue:@"application/json;encoding:utf-8"];
    //设置发送的数据为json格式
    
    [operation setPostDataEncoding:MKNKPostDataEncodingTypeJSON];
    [operation addCompletionHandler:^(MKNetworkOperation *completedOperation)
     {
         
         if([completedOperation isCachedResponse]) {
             // DLog(@"Data from cache %@", [completedOperation responseString]);
         }
         else {
             //DLog(@"Data from server %@", [completedOperation responseString]);
         }
         //返回数据
         completionBlock(completedOperation);
         
     }errorHandler:^(MKNetworkOperation *errorOp, NSError* error) {
         //DLog(@"REQUEST ERROR:\n%@",[[errorOp readonlyRequest] allHTTPHeaderFields ]);
         errorBlock(error);
     }];
    
    [self enqueueOperation:operation];

    return operation;

}




@end
